﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ENG.TeachersDiary
{
  static class Exporter
  {
    private const string EVEN = "even";
    private const string ODD = "odd";

    public static void ExportStudentsToTxt(string fileName)
    {
      StreamWriter str = new StreamWriter(fileName, true);

      var db = Common.DBConn;
      var students = Common.GetCurrentClass(db).Students.OrderBy(i => i.SurnameName);

      foreach (var fItem in students)
      {
        str.WriteLine(fItem.SurnameName);
      } // foreach (var fItem in students)

      str.Close();
    }

    public static void ExportStudentsToHtml(string fileName)
    {
      StreamWriter str = new StreamWriter(fileName, false);

      var db = Common.DBConn;
      var currentClass = Common.GetCurrentClass(db);

      BuildHtmlHeader(str, Properties.Resources.ListOfStudentsHTML + " " + currentClass.Title);

      str.WriteLine("<h1>" + Properties.Resources.ListOfStudentsHTML + " "+ currentClass.Title + "</h1>");

      str.WriteLine("<table>");

      bool isEven = false;

      var students = currentClass.Students.OrderBy(i => i.SurnameName);
      foreach (var fItem in students)
      {       
        str.WriteLine("<tr class=\"" + (isEven ? EVEN: ODD) +  "\"><td>");
        str.WriteLine(fItem.SurnameName);
        str.WriteLine("</td></tr>");
      } // foreach (var fItem in students)

      str.WriteLine("</table>");

      BuildHtmlFooter(str);

      str.Close();
    }

    private static void BuildHtmlHeader(System.IO.StreamWriter str, string htmlPageTitle)
    {
      str.WriteLine(
        "<html><head>");

      str.WriteLine("<title>" + htmlPageTitle + "</title>");
      str.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");

      string css = Properties.Settings.Default.CssStyleFile;

      if (!string.IsNullOrEmpty(css))
      {
        if ((css.Length > 2) && (css[1] != ':'))
          css = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, css);
        if (System.IO.File.Exists(css))
          str.WriteLine("<style>");
        System.IO.StreamReader strR = new System.IO.StreamReader(css);
        str.WriteLine(strR.ReadToEnd());
        strR.Close();
        str.WriteLine("</style>");
      }

      str.WriteLine("</head><body>");
    }

    private static void BuildHtmlFooter(System.IO.StreamWriter str)
    {
      str.WriteLine(
        "<br /><br /><small class=\"genFooter\">Generated " + DateTime.Now.ToString() +" by ENG Teachers Diary </small></body></html>");
    }

    public static void ExportClassMarksToHtml(string fileName)
    {
      var db = Common.DBConn;
      var currentClass = Common.GetCurrentClass(db);

      StreamWriter str = new StreamWriter(fileName, false);

      BuildHtmlHeader(str, Properties.Resources.ClassMarksHTML + currentClass.Title);

      str.WriteLine("<h1>" + Properties.Resources.ClassMarksHTML+ " " + currentClass.Title + "</h1>");

      str.WriteLine("<table>");

      var markGs = Common.GetCurrentSubject(db).MarkGroups.Where(i => i.IsToOffer).OrderBy(i => i.OrderIndex);

      str.WriteLine("<tr>");
      str.WriteLine("<th>#</th>");
      str.WriteLine("<th>" + Properties.Resources.SurnameNameHTML +"</th>");
      foreach (var fItem in markGs)
      {
        str.WriteLine("<th>" + fItem.ExtTitle + "</th>");
      } // foreach (var fItem in markGs)
      str.WriteLine("<th>" + Properties.Resources.MeanGridColumn +"</th>");
      str.WriteLine("<th>" + Properties.Resources.DisplayMarkGridColumn +"</th>");
      str.WriteLine("</tr>");

      Classes.StudentResultCollection ress = Classes.StudentResultCollection.CreateDefault2();
      int index = 0;
      foreach (var fItem in ress)
      {
        index++;
        str.WriteLine("<tr class=\"" + (index % 2 == 0 ? EVEN:ODD) + "\">");

        str.WriteLine("<td class=\"studentIndexColumn\">" + index + "</td>");
        str.WriteLine("<td class=\"studentNameColumn\">" + fItem.Student.SurnameName + "</td>");

        foreach (var xItem in markGs)
        {
          Classes.MarkGroupResult mgr = fItem.GetByMarkGroup(xItem);

          str.WriteLine("<td>" + mgr.DisplayMarksString + "</td>");

        } // foreach (var xItem in fItem.Results)

        str.WriteLine("<td class=\"sumMeanColumn\">" + 
          fItem.SumResult.Value.ToString(Properties.Settings.Default.NumberFormat) + "</td>");
        str.WriteLine("<td class=\"sumMarkColumn\">" + fItem.SumResult.Display + "</td>");

        str.WriteLine("</tr>");
      } // foreach (var fItem in students)

      str.WriteLine("</table>");

      BuildHtmlFooter(str);

      str.Close();
    }

    internal static void ExportStudentMarksToHtml(int studentId, string fileName)
    {
      var db = Common.DBConn;

      var student = db.Students.First(i => i.StudentID == studentId);

      StreamWriter str = new StreamWriter(fileName, false);

      BuildHtmlHeader(str, Properties.Resources.MarksOfStudentHTML + " " + student.NameSurname);

      str.WriteLine("<h1>" + Properties.Resources.MarksOfStudentHTML + " " + student.NameSurname + "</h1>");

      str.WriteLine("<table>");

      str.WriteLine("<tr>");
      str.WriteLine("<th>" +Properties.Resources.DateHTML + "</th>");
      str.WriteLine("<th>" + Properties.Resources.MarkGroupHTML +"</th>");
      str.WriteLine("<th>" + Properties.Resources.MarkHTML + "</th>");
      str.WriteLine("<th>" + Properties.Resources.NoteHTML +"</th>");
      str.WriteLine("</tr>");

      var marks = student.Marks.OrderBy(i => i.Date);

      bool isEven = false;
      foreach (var fItem in marks)
      {
        str.WriteLine("<tr class=\"" + (isEven ? EVEN : ODD) + "\">");

        str.WriteLine("<td class=\"markDateColumn\">" + fItem.Date.ToString() + "</td>");
        str.WriteLine("<td class=\"markGroupColumn\">" + fItem.MarkGroup.ExtTitle + "</td>");
        str.WriteLine("<td class=\"markGroupColumn\">" + fItem.MarkCode.Display + "</td>");
        str.WriteLine("<td class=\"markGroupColumn\">" + fItem.Note + "</td>");

        str.WriteLine("</tr>");

        isEven = !isEven;
      } // foreach (var fItem in students)

      str.WriteLine("</table>");

      BuildHtmlFooter(str);

      str.Close();
    }
  }
}
